package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.domain.ResponseResult;
import org.example.domain.dto.CommentListDto;
import org.example.domain.entity.Comment;
import org.example.domain.entity.User;
import org.example.domain.vo.CommentVo;
import org.example.domain.vo.PageVo;
import org.example.mapper.CommentMapper;
import org.example.mapper.UserMapper;
import org.example.service.CommentService;
import org.example.utils.BeanCopyUtils;
import org.example.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private final CommentMapper commentMapper;

    private final UserMapper userMapper;

    @Override
    public ResponseResult sendComment(Comment comment) {
        Integer id = SecurityUtils.getUserId();
        comment.setCreateBy(id);
        save(comment);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCommentList(CommentListDto commentListDto) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getType,0);
        queryWrapper.eq(Comment::getRootId,-1);
        queryWrapper.eq(Comment::getArticleId,commentListDto.getArticleId());

        Page<Comment> page = new Page(commentListDto.getPageNum(),commentListDto.getPageSize());
        page(page,queryWrapper);

        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(page.getRecords(), CommentVo.class);
        for (CommentVo commentVo : commentVos) {
            User user = userMapper.SelectByUserId(commentVo.getCreateBy());
            if(user != null){
                commentVo.setAvatar(user.getAvatar());
                commentVo.setUserNickname(user.getNickName());
            }
            List<Comment> comments = commentMapper.getChildrenComment(commentVo.getId());
            List<CommentVo> commentChildren = BeanCopyUtils.copyBeanList(comments, CommentVo.class);
            for (CommentVo commentVo1 : commentChildren) {
                User user1 = userMapper.SelectByUserId(commentVo1.getToCommentUserId());
                User user2 = userMapper.SelectByUserId(commentVo1.getCreateBy());
                if(user1 != null){
                    commentVo1.setToCommentNickname(user1.getNickName());
                }
                if(user2 != null){
                    commentVo1.setAvatar(user2.getAvatar());
                    commentVo1.setUserNickname(user2.getNickName());
                }
            }
            commentVo.setChildren(commentChildren);
        }
        return ResponseResult.okResult(new PageVo(commentVos,page.getTotal()));
    }

    @Override
    public ResponseResult deleteComment(Integer id) {
        commentMapper.deleteCommentById(id);
        return ResponseResult.okResult();
    }
}
